package com.fyyg.common.util;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.StringUtil;

public class ArrayUtil {

	
	/**
	 * 数组是否存在某个值
	 * @param arr
	 * @param targetValue
	 * @return
	 */
	public static boolean arrayExist(String[] arr, String targetValue) {
		try {
			if(arr.length>0 && StringUtil.isNotEmpty(targetValue)){
				Set<String> set = new HashSet<String>(Arrays.asList(arr));
				return set.contains(targetValue);
			}else{
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
    }
	
	
	/**
	 * 字符串是否存在某个值
	 * @param one
	 * @param two
	 * @return
	 */
	public static boolean StringExist(String one,String two){
		try {
			if(StringUtil.isNotEmpty(one) && StringUtil.isNotEmpty(two)){
				String[] arr = one.split(",");
				return arrayExist(arr,two);
			}else{
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	/**
	 * json是否存某个值
	 * @param one
	 * @param two
	 * @return
	 */
	public static int JsonExist(String json ,String two){
		if(StringUtil.isEmpty(json) || StringUtil.isEmpty(two)){
			return 0;
		}
		try {
			JSONObject JSON = new JSONObject().parseObject(json);
			if(null == JSON.get(two)){
				return 0;
			}else{
				return Integer.parseInt(JSON.get(two).toString());
			}
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}
	
	
	
	public static void main(String[] args) {
		String a="{11:3,18:3,23:3}";
		System.out.println(JsonExist(a,"0"));
	}
}
